package com.sctvcloud.qingchuan.ui.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.application.Cache;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.base.BaseActivity;
import com.sctvcloud.qingchuan.beans.PoliticsAccountBean;
import com.sctvcloud.qingchuan.beans.PoliticsScreenCondition;
import com.sctvcloud.qingchuan.beans.PoliticsStatusBean;
import com.sctvcloud.qingchuan.http.AbsNetCallBack;
import com.sctvcloud.qingchuan.http.NetUtils;
import com.sctvcloud.qingchuan.ui.utils.DataUtils;
import com.sctvcloud.qingchuan.ui.utils.TitleUtils;
import com.sctvcloud.qingchuan.utils.ListTypeUtils;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * 选择筛选条件
 */
public class ScreenConditionActivity extends BaseActivity {

    @BindView(R.id.status_flow)
    protected TagFlowLayout statusFlow;
    @BindView(R.id.object_flow)
    protected TagFlowLayout objectFlow;
    @BindView(R.id.title_top_edit_topic)
    protected CustomFontTextView confirm;

    private LayoutInflater mInflater;
    private ArrayList<PoliticsStatusBean> status = new ArrayList<>();
    private TagAdapter<PoliticsStatusBean> statusTagAdapter;
    private PoliticsStatusBean selectedStatus;

    private ArrayList<PoliticsAccountBean> objectDataList = new ArrayList<>();
    private TagAdapter<PoliticsAccountBean> objTagAdapter;
    private PoliticsAccountBean selectedObj;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle("筛选");
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_screen_condition);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        PoliticsScreenCondition condition = Cache.getInstance().getPcCondition();
        if (null != condition) {
            selectedObj = condition.getPoliticsObject();
            selectedStatus = condition.getStatus();
        }
    }

    private void initView() {
        confirm.setVisibility(View.VISIBLE);
        confirm.setText("确定");
        mInflater = LayoutInflater.from(this);
        statusTagAdapter = new TagAdapter<PoliticsStatusBean>(status) {

            @Override
            public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, PoliticsStatusBean s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.condition_flow_text,
                        statusFlow, false);
                tv.setText(s.getStatusStr());
                return tv;
            }

            @Override
            public boolean setSelected(int position, PoliticsStatusBean s) {
                return false;
            }
        };
        statusFlow.setAdapter(statusTagAdapter);
        statusFlow.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, com.zhy.view.flowlayout.FlowLayout parent) {
                return true;
            }
        });
        statusFlow.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                if (null != selectPosSet && selectPosSet.size() > 0) {
                    for (Integer i : selectPosSet) {
                        selectedStatus = statusTagAdapter.getItem(i);
                    }
                } else {
                    selectedStatus = null;
                }

            }

        });


        objTagAdapter = new TagAdapter<PoliticsAccountBean>(objectDataList) {

            @Override
            public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, PoliticsAccountBean s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.condition_flow_text,
                        objectFlow, false);
                tv.setText(s.getUserName());
                return tv;
            }

            @Override
            public boolean setSelected(int position, PoliticsAccountBean s) {
                return false;
            }
        };
        objectFlow.setAdapter(objTagAdapter);
        objectFlow.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, com.zhy.view.flowlayout.FlowLayout parent) {
                return true;
            }
        });
        objectFlow.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                if (null != selectPosSet && selectPosSet.size() > 0) {
                    for (Integer i : selectPosSet) {
                        selectedObj = objTagAdapter.getItem(i);
                    }
                } else {
                    selectedObj = null;
                }

            }

        });
        initStatusData();
        getObjData();
    }

    private void getObjData() {
        Map<String, Object> formMaildata = new HashMap<>();
//        formMaildata.put("tagId", tagId);
//        if (!TextUtils.isEmpty(key)) {
//            formMaildata.put("userNameByLike", key);
//        }
        RequestBody body = DataUtils.toRequestBody(formMaildata);
        NetUtils.getNetAdapter().toGetPoliticsAccount(getOwnerName(), Constances.TENANT_ID, body, new AbsNetCallBack<String>(String.class) {
            @Override
            public void onSucStr(String news) {
                if (!TextUtils.isEmpty(news)) {
                    try {
                        List<PoliticsAccountBean> list = gson.fromJson(news, ListTypeUtils.PoliticsAccountBean());
                        if (ListUtils.isListValued(list)) {
                            PoliticsAccountBean bean = new PoliticsAccountBean();
                            bean.setAuthorId(0);
                            bean.setUserName("全部");
                            objectDataList.add(bean);
                            objectDataList.addAll(list);
                            initObjData();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    onError(null, "数据异常");
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                toast(errorMsg);

            }


            @Override
            public void onEnd() {

            }
        });


    }

    private void initStatusData() {
//        (3,"处理中"),(4,"已回复"),(7,"已驳回"),(8,"已转发到相关部门")
        PoliticsStatusBean sBean = new PoliticsStatusBean();
        sBean.setStatus(0);
        sBean.setStatusStr("全部");
        status.add(sBean);
        sBean = new PoliticsStatusBean();
        sBean.setStatus(3);
        sBean.setStatusStr("处理中");
        status.add(sBean);
        sBean = new PoliticsStatusBean();
        sBean.setStatus(4);
        sBean.setStatusStr("已回复");
        status.add(sBean);
        sBean = new PoliticsStatusBean();
        sBean.setStatus(7);
        sBean.setStatusStr("已驳回");
        status.add(sBean);
        PoliticsScreenCondition condition = Cache.getInstance().getPcCondition();
        if (condition != null && condition.getStatus() != null && condition.getStatus().getStatus() != 0) {
            for (int i = 0; i < status.size(); i++) {
                if (status.get(i).getStatus() == condition.getStatus().getStatus()) {
                    statusTagAdapter.setSelectedList(i);//默认选中第一个
                    break;
                }
            }
        } else {
            statusTagAdapter.setSelectedList(0);//默认选中第一个
        }
    }

    private void initObjData() {

        PoliticsScreenCondition condition = Cache.getInstance().getPcCondition();
        if (condition != null && condition.getPoliticsObject() != null && condition.getPoliticsObject().getAuthorId() != 0) {
            for (int i = 0; i < objectDataList.size(); i++) {
                if (objectDataList.get(i).getAuthorId() == condition.getPoliticsObject().getAuthorId()) {
                    objTagAdapter.setSelectedList(i);//默认选中第一个
                    break;
                }
            }
        } else {
            objTagAdapter.setSelectedList(0);//默认选中第一个
        }
    }

    @OnClick(value = {R.id.title_top_edit_topic})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.title_top_edit_topic:
                PoliticsScreenCondition condition = Cache.getInstance().getPcCondition();
                if (null == condition) {
                    condition = new PoliticsScreenCondition();
                }
                condition.setPoliticsObject(selectedObj);
                condition.setStatus(selectedStatus);
                Cache.getInstance().setPcCondition(condition);
                setResult(RESULT_OK);
                finish();
                break;
        }
    }
}
